package com.ncloud.resman.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ncloud.common.body.Result;
import com.ncloud.common.utils.ResultUtil;
import com.ncloud.resman.entity.SwitchSubAssign;
import com.ncloud.resman.service.impl.SwitchSubAssignServiceImpl;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
/**
 * Created by gaowei on 2017/8/31.
 */
@RestController
@RequestMapping("/resman/switchsub")
public class SwitchSubAssignController {
	@Autowired
	private SwitchSubAssignServiceImpl switchSubAssignService;
    @GetMapping(value = "/list")
    public Result<?> getCabinetInfo(HttpServletRequest request){
    	JSONObject params = new JSONObject();
    	JSONObject infos = switchSubAssignService.getSwitchInfo(params);
		return ResultUtil.success(infos);
    }
    @GetMapping(value = "/assignlist/{deviceId}")
    public Result<?> getSwitchSubAssignInfo(HttpServletRequest request,@PathVariable("deviceId") Long deviceId){
    	JSONObject params = new JSONObject();
    	params.put("deviceId", deviceId);
    	JSONObject infos = switchSubAssignService.getSwitchSubAssignInfo(params);
		return ResultUtil.success(infos);
    }
    @PostMapping(value = "/edit")
    public Result<?> add(HttpServletRequest request, @RequestBody SwitchSubAssign switchSubAssign){
    	Object userID = request.getAttribute("userID");
    	switchSubAssign.setCreateUser(userID==null?1:Long.parseLong(userID.toString()));
    	switchSubAssign.setCreateDate(new Date());
    	switchSubAssign.setUpdateUser(userID==null?1:Long.parseLong(userID.toString()));
    	switchSubAssign.setUpdateDate(new Date());
    	int num = switchSubAssignService.editSwitchSubAssign(switchSubAssign);
		return ResultUtil.success(num);
    }
    @DeleteMapping(value = "/delete/{deviceId}/{subNo}")
    public Result<?> delete(HttpServletRequest request, @PathVariable("deviceId") Long deviceId,@PathVariable("subNo") int subNo){
    	int num = switchSubAssignService.deleteSwitchSubAssign(deviceId, subNo);
		return ResultUtil.success(num);
    }
    @GetMapping(value = "/roomlist")
    public Result<?> getRoomAll(HttpServletRequest request){
    	JSONObject params = new JSONObject();
    	JSONArray infos = switchSubAssignService.getRoomAll(params);
		return ResultUtil.success(infos);
    }
    @GetMapping(value = "/cabinetlist/{roomId}")
    public Result<?> getCabinetByRoomId(HttpServletRequest request,@PathVariable("roomId") Integer roomId){
    	JSONArray infos = switchSubAssignService.getCabinetByRoomId(roomId);
		return ResultUtil.success(infos);
    }
    @GetMapping(value = "/iplist/{cabinetId}")
    public Result<?> getManageIpByCabinetId(HttpServletRequest request,@PathVariable("cabinetId") Long cabinetId){
    	JSONArray infos = switchSubAssignService.getManageIpByCabinetId(cabinetId);
		return ResultUtil.success(infos);
    }
    @GetMapping(value = "/sublist/{deviceId}/{eqptType}")
    public Result<?> getSubNoById(HttpServletRequest request,@PathVariable("deviceId") Long deviceId,@PathVariable("eqptType") String eqptType){
    	JSONArray infos = switchSubAssignService.getSubNoById(deviceId, eqptType);
		return ResultUtil.success(infos);
    }
    /*@PutMapping(value = "/update")
    public Result<?> update(HttpServletRequest request, @RequestBody Cabinet cabinet){
    	Object userID = request.getAttribute("userID");
    	cabinet.setUpdateUser(userID==null?1:Long.parseLong(userID.toString()));
    	cabinet.setUpdateDate(new Date());
    	int num = cabinetService.updateCabinet(cabinet);
		return ResultUtil.success(num);
    }
    
    @PostMapping(value = "/import")
    public Result<?> importCabinet(HttpServletRequest request, @RequestParam(value="file",required=false) MultipartFile file) throws IOException{
    	Object userID = request.getAttribute("userID");
    	Long userId = userID==null?1:Long.parseLong(userID.toString());
    	int num = cabinetService.importCabinet(userId,file.getInputStream());
		return ResultUtil.success(num);
    }
    @GetMapping(value = "/export")
    public Result<?> exportCabinet(HttpServletRequest request,HttpServletResponse response) throws IOException{
    	JSONObject params = new JSONObject();
    	int exportNum=cabinetService.exportCabinet(params, response);
		return ResultUtil.success(exportNum);
    }
    @GetMapping(value = "/exporttemplate")
    public Result<?> exportTemplate(HttpServletRequest request,HttpServletResponse response) throws IOException{
    	int fileNum=cabinetService.exportTemplate(response);
		return ResultUtil.success(fileNum);
    }*/
}
